package sk.stuba.fiit.npr.console;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class ConsoleManager {
	
	public static String writeAndSelectParsedLexicalUnits(Map<String, String> parsedSimpleURL) {
		Map<String, String> keysToLowercase = new HashMap<String, String>();
		List<String> keys = new ArrayList<String>(parsedSimpleURL.keySet());
        Collections.sort(keys);
        for(String s : keys) {
            System.out.println(s + ": " + parsedSimpleURL.get(s));
        }
        
        //convert keys to lowercase
        for(String key : keys) {
        	keysToLowercase.put(key.toLowerCase(), key);
        }
        
        String input = catchUserInput();
        //according to lowecase input select original key 
        String originalKey = keysToLowercase.get(input.toLowerCase());
        if(originalKey != null) {
        	return parsedSimpleURL.get(originalKey);
        } else {
        	System.out.println("--- INCORRECT INPUT --- ");
        	return writeAndSelectParsedLexicalUnits(parsedSimpleURL); //call method recursively
        }
	}
	
	public static String selectNextStep(int item, Map<String, String> parsedSimpleURL) {
		switch(item) {
			case 0:
				return writeAndSelectParsedLexicalUnits(parsedSimpleURL);
			case 1:
				System.out.println("Zadajte URL");
				return catchUserInput();
			default:
				System.out.println("Incorrect input");
				return showMenu(parsedSimpleURL);
		}
	}
	
	public static String showMenu(Map<String, String> parsedSimpleURL) {
		System.out.println("--- MENU ---");
		System.out.println("0 - Vybrat URL z prikladov");
		System.out.println("1 - Zadat vlastnu URL");
		return selectNextStep(Integer.valueOf(catchUserInput()), parsedSimpleURL);
	}
	
	private static String catchUserInput() {
		Scanner scannerIn = new Scanner(System.in);
		return scannerIn.nextLine();
	}
	
	public static boolean continueWithNextURL() {
		System.out.println("Chcete pokracovat s dalsou URL?");
		System.out.println("0 - ano");
		System.out.println("1 - nie");
		int i = Integer.valueOf(catchUserInput());
		switch(i) {
			case 0:
				return true;
			case 1:
				return false;
			default:
				return false;
		}
		
	}
}
